//Portions Copyright (C) 2011 Randall Shattuck
//Portions Copyright (C) 2007-2010 Owners of libeveapi 
//    (http://libeveapi.googlecode.com)
//
//EveDashboard.Api was forked from libeveapi 1.1.3
//
//This program is free software; you can redistribute it and/or modify it under
//the terms of the GNU General Public License as published by the Free Software 
//Foundation; either version 2 of the License, or (at your option) any later 
//version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT 
//ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
//FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
//details. You should have received a copy of the GNU General Public License 
//along with this program; if not, write to the Free Software Foundation, Inc.,
//59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;

namespace EveDashboard.Api.ResponseObjects.Parsers
{
    ///<summary>
    /// A parser which converts a given <see cref="XmlDocument"/> to a <see cref="CharacterIdName"/>.
    ///</summary>
    internal class CharacterIdNameResponseParser : IApiResponseParser<CharacterIdName>
    {
        public CharacterIdName Parse(XmlDocument xmlDocument)
        {
            this.CheckVersion(xmlDocument);

            //FIX the row:name problem here (HACK!)
            string fixXML = xmlDocument.OuterXml;
            fixXML = Regex.Replace(fixXML, "row:name", "row");
            xmlDocument.LoadXml(fixXML);
            //END FIX

            //REAL FIX - We're not using this because if they change then this will break -
            /* XmlNamespaceManager namespaceManager = new XmlNamespaceManager( xmlDocument.NameTable);
             * namespaceManager.AddNamespace("row", "characterId");
             * XmlNode n = d.SelectSingleNode("//rowset[@name='characters']/row:name", namespaceManager);
             * Console.WriteLine(n.Attributes["name"].InnerText + " " + n.Attributes["characterID"].InnerText)
             */

            CharacterIdName charId = new CharacterIdName();
            charId.ParseCommonElements(xmlDocument);

            List<CharacterIdName.CharacterIdNameItem> characterList = new List<CharacterIdName.CharacterIdNameItem>();
            foreach (XmlNode charIdRow in xmlDocument.SelectNodes("//rowset[@name='characters']/row"))
            {
                CharacterIdName.CharacterIdNameItem charIdItem = new CharacterIdName.CharacterIdNameItem();
                charIdItem.Name = charIdRow.Attributes["name"].InnerText;
                charIdItem.CharacterId = Convert.ToInt32(charIdRow.Attributes["characterID"].InnerText, CultureInfo.InvariantCulture);
                characterList.Add(charIdItem);
            }
            charId.CharacterIdItems = characterList.ToArray();

            return charId;
        }

        public void CheckVersion(XmlDocument xmlDocument)
        {
            if (CharacterIdName.VersionCheck)
            {
                string version = xmlDocument.SelectSingleNode("//eveapi").Attributes["version"].InnerText;
                if (version.CompareTo(CharacterIdName.API_VERSION) != 0)
                {
                    throw new ApiVersionException(version, CharacterIdName.API_VERSION);
                }
            }
        }

    }
}
